package com.ct.application.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.goods.convert.GoodsConvert;
import com.ct.application.goods.model.po.GoodsQueryPO;
import com.ct.application.goods.model.po.GoodsSeckillUpdatePO;
import com.ct.application.goods.model.vo.GoodsSeckillVO;
import com.ct.goods.orm.Goods;
import com.ct.goods.orm.GoodsSeckill;
import com.ct.goods.responsity.admin.IAdminGoodsRepository;
import com.ct.goods.responsity.admin.IAdminGoodsSeckillRepository;
import com.ct.repository.commons.PageUtils;
import com.ct.repository.convert.CopyUtils;
import com.ct.utils.valid.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 秒杀商品 服务实现类
 *
 * @author system
 */
@Service
public class GoodsSeckillService {


    @Resource
    private IAdminGoodsSeckillRepository goodsSeckillDao;
    @Resource
    private IAdminGoodsRepository goodsDao;
    @Resource
    private GoodsConvert goodsConvert;


    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return goodsSeckillDao.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return goodsSeckillDao.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    /**
     * 获取秒杀商品列表数据
     */
    public PageUtils<GoodsSeckillVO> getSeckillGoodsList(GoodsQueryPO queryPO) {
        LambdaQueryWrapper<GoodsSeckill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(queryPO.getKeyword()), GoodsSeckill::getGoodsName, queryPO.getKeyword());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getStatus()), GoodsSeckill::getStatus, queryPO.getStatus());
        lambdaQueryWrapper.orderByDesc(GoodsSeckill::getCreateTime);
        Page<GoodsSeckill> page = new Page<>(queryPO.getPageNo(), queryPO.getPageSize());
        IPage<GoodsSeckill> goodsIPage = goodsSeckillDao.page(page, lambdaQueryWrapper);

        List<GoodsSeckill> goodsSeckillList = goodsIPage.getRecords();
        if (goodsSeckillList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }
        List<GoodsSeckillVO> goodsSeckillVOList = goodsConvert.toGoodsSeckillVOList(goodsSeckillList);
        return new PageUtils<>(goodsIPage, goodsSeckillVOList);
    }


    /**
     * 根据id查询秒杀商品
     */
    public GoodsSeckillVO getSeckillGoodsById(Long id) {
        GoodsSeckill goodsSeckill = goodsSeckillDao.getById(id);
        if (goodsSeckill == null) {
            return null;
        }
        return CopyUtils.copyProperties(goodsSeckill,GoodsSeckillVO.class);
    }

    /**
     * 添加为秒杀商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Long goodsId) {
        Goods goods = goodsDao.getById(goodsId);
        if (goods == null) {
            throw new RuntimeException("商品不存在");
        }
        GoodsSeckill dbGoodsSeckill = goodsSeckillDao.getByGoodsId(goodsId);
        if (dbGoodsSeckill != null) {
            throw new RuntimeException("已存在相同秒杀商品");
        }

        GoodsSeckill goodsSeckill = new GoodsSeckill();
        goodsSeckill.setGoodsId(goods.getId());
        goodsSeckill.setGoodsName(goods.getName());
        goodsSeckill.setMainImage(goods.getMainImage());
        goodsSeckill.setShopsName(goods.getShopsName());
        goodsSeckill.setShopsId(goods.getShopsId());
        goodsSeckill.setOriginalPrice(goods.getOriginalPrice());
        goodsSeckill.setPrice(goods.getPrice());
        goodsSeckill.setStock(goods.getStock());
        goodsSeckill.setStock(goods.getStatus());
        return goodsSeckill.insert();
    }


    /**
     * 修改秒杀商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(GoodsSeckillUpdatePO po) {
        Long goodsSeckillId = po.getId();
        GoodsSeckill goodsSeckill;
        if (goodsSeckillId == null || (goodsSeckill = goodsSeckillDao.getById(goodsSeckillId))== null) {
            throw new RuntimeException("秒杀商品不存在");
        }
        Goods goods = goodsDao.getById(goodsSeckill.getGoodsId());
        if (goods == null) {
            throw new RuntimeException("商品不存在");
        }

        return goodsSeckillDao.update(po.getId(),po.getOriginalPrice(),po.getPrice()
                ,po.getStock(),po.getStartTime(),po.getEndTime());
    }




    /**
     * 删除秒杀商品
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return goodsSeckillDao.removeById(id);
    }
}
